<?php

namespace App\Http\Controllers\Api\V1\Frontend\Auth;

use App\Http\Requests\Api\V1\Frontend\Auth\FastLoginRequest;
use App\Http\Requests\Api\V1\Frontend\Auth\Index\AccountCheckRequest;
use App\Http\Requests\Api\V1\Frontend\Auth\Index\RegisterRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Users;
use Illuminate\Support\Facades\Redis;
use App\Enums\RedisKeyEnum;
use Illuminate\Support\Facades\Gate;

use App\Models\UsersInfo;


class IndexController extends Controller
{
    public function gates()
    {

        dd(2222);
    }

    public function demo(Request $request)
    {
        $userInfo = UsersInfo::where('user_id',$request->input('user_id'))->first();

        $response = Gate::inspect('update', $userInfo);

        if ($response->allowed()) {
            dd(1111);
        } else {
            dd($response->message());
        }
    }


































    public function accountCheck(AccountCheckRequest $request)
    {
        $redisKey = RedisKeyEnum::ACCOUNT_KEY_USERNAME;

        if (!Redis::exists($redisKey)){

        }

        if (Redis::sismember($redisKey,$request->input('username'))){
            return $this->failed('账号已被注册');
        }

        return $this->message('账号可用');
    }

    public function createUserUserNameRedisCache()
    {
        $redisKey = RedisKeyEnum::ACCOUNT_KEY_USERNAME;

        Users::pluck('username')->map(function ($value){
//            Redis::sadd()
        });

    }

    public function register(RegisterRequest $request)
    {
        if ($this->getUserByAccount('username',$request->input('username'))){
            return $this->failed('用户名已注册');
        }

        if ($this->getUserByAccount('phone',$request->input('phone'))){
            return $this->failed('手机号注册');
        }

        try{
            Users::create([
                'name'     => '用户'.custom_random_code(8),
                'username' => $request->input('username'),
                'phone'    => $request->input('phone'),
                'password' => bcrypt($request->input('password'))
            ]);

            return $this->message('注册成功');
        }catch (\Exception $exception){
            return $this->failed('注册失败');
        }
    }



































































    /**
     * @Notes: 快速登录
     *
     * @param FastLoginRequest $request
     * @return mixed
     * @Author: smile
     * @Date: 2021/4/27
     * @Time: 19:50
     */
    public function fastLogin(FastLoginRequest $request)
    {
        if (! $user = $this->getUserByAccount('phone',$request->input('phone'))){
            return $this->failed('手机号未注册，请先注册');
        }

        if ($user->status == 2){
            return $this->failed('账号异常，请联系管理员');
        }

        $token = auth()->login($user);

        return $this->successful([
            'token' => 'Bearer '.$token
        ],'登录成功');
    }

    /**
     * @Notes:普通登录
     *
     * @param Request $request
     * @return mixed
     * @Author: smile
     * @Date: 2021/4/27
     * @Time: 19:31
     */
    public function commonLogin(Request $request)
    {
        $type  = custom_account_type($request->input('account'));

        $credentials = [
            'password' => $request->input('password'),
            $type      => $request->input('account')
        ];

        if (!$token = auth()->attempt($credentials)){
            if (!$this->getUserByAccount($type,$request->input('account'))){
                return $this->failed(trans('account.'.$type));
            }

            return $this->failed('账号密码错误');
        }

        if ($this->getUserByAccount($type,$request->input('account'))->status == 2){
            return $this->failed('账号异常');
        }

        return $this->successful([
            'token' => 'Bearer '.$token
        ],'登录成功');
    }

    /**
     * @Notes: 获取到user的信息
     *
     * @param string $type
     * @param $account
     * @return mixed
     * @Author: smile
     * @Date: 2021/4/27
     * @Time: 19:49
     */
    private function getUserByAccount(string $type,$account)
    {
        return Users::where($type,$account)
            ->first();
    }

    public function socialiteLogin()
    {

    }

    public function logout()
    {
        auth()->logout();

        return $this->success('安全退出');
    }
}
